package com.threeox.commonlibrary.ui.activity.base;

import android.annotation.TargetApi;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.support.annotation.NonNull;
import android.view.KeyEvent;
import android.view.KeyboardShortcutGroup;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;

import com.threeox.commonlibrary.entity.engine.KeyEventConstant;
import com.threeox.commonlibrary.entity.engine.Lifecycle;
import com.threeox.commonlibrary.entity.engine.model.base.BaseModelMsg;
import com.threeox.commonlibrary.ui.view.engineview.base.CommonModelView;
import com.threeox.commonlibrary.ui.view.inter.engine.base.IBaseExtend;

import java.util.List;

/**
 *
 * @ClassName: BaseModelActivity
 *
 * @Description: TODO()
 *
 * @author 赵屈犇
 *
 * @date 创建时间: 2017/7/5 14:20
 *
 * @version 1.0
 *
 */
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public abstract class BaseModelActivity extends CommonActivity {

    protected IBaseExtend mBaseExtend;
    protected CommonModelView mCommonModelView;

    /**
     * 初始化是否全屏
     *
     * @param modelView
     */
    protected void initFullScreenState(CommonModelView modelView) {
        if (modelView != null) {
            BaseModelMsg modelMsg = modelView.initModelMessage(modelView.getFileName(getIntent()));
            if (modelMsg != null) {
                if (modelMsg.isFullscreen()) {// 设置全屏
                    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
                } else {
                    requestWindowFeature(Window.FEATURE_NO_TITLE);
                }
                if (modelMsg.isFullScreenShowStatusBar()) {// 设置全屏屏显示标题栏
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
                }
            }
        }
    }

    /**
     * 初始化信息
     *          不要放在setView中调用
     * @param modelMessage
     * @param viewGroup
     */
    protected void initBaseModelMessage(BaseModelMsg modelMessage, CommonModelView viewGroup) {
        if (modelMessage != null) {
            if (mTintManager != null) {
                mTintManager.setStatusBarTintEnabled(modelMessage.isStatusBarTintEnabled());// 设置是否启用系统状态栏着色
                mTintManager.setNavigationBarTintEnabled(modelMessage.isNaviBarTintEnabled());// 设置系统导航栏是否启用着色
            }
        }
        if (viewGroup != null) {
            viewGroup.setClipToPadding(true);
            viewGroup.setFitsSystemWindows(true);
        }
        this.mCommonModelView = viewGroup;
        this.mBaseExtend = viewGroup.getBaseExtend();
        if (null != mBaseExtend) {
            mBaseExtend.initActivity(this);
        }
    }

    public <T extends IBaseExtend> T getBaseExtend() {
        return (T) mBaseExtend;
    }

    public <T extends CommonModelView> T getCommonModelView() {
        return (T) mCommonModelView;
    }

    /*============================生命周期===========================*/
    @Override
    protected void onStart() {
        boolean result = true;
        if (null != mCommonModelView) {
            result = mCommonModelView.onLifecycle(Lifecycle.onStart);
        }
        if (result) {
            super.onStart();
        }
    }

    @Override
    protected void onPause() {
        boolean result = true;
        if (null != mCommonModelView) {
            result = mCommonModelView.onLifecycle(Lifecycle.onPause);
        }
        if (result) {
            super.onPause();
        }
    }

    @Override
    protected void onStop() {
        boolean result = true;
        if (null != mCommonModelView) {
            result = mCommonModelView.onLifecycle(Lifecycle.onStop);
        }
        if (result) {
            super.onStop();
        }
    }

    @Override
    protected void onResume() {
        boolean result = true;
        if (null != mCommonModelView) {
            result = mCommonModelView.onLifecycle(Lifecycle.onResume);
        }
        if (result) {
            super.onResume();
        }
    }

    @Override
    protected void onRestart() {
        boolean result = true;
        if (null != mCommonModelView) {
            result = mCommonModelView.onLifecycle(Lifecycle.onRestart);
        }
        if (result) {
            super.onRestart();
        }
    }

    @Override
    protected void onDestroy() {
        boolean result = true;
        if (null != mCommonModelView) {
            result = mCommonModelView.onLifecycle(Lifecycle.onDestroy);
        }
        if (result) {
            super.onDestroy();
        }
    }

    /*============================生命周期===========================*/

    /*====================监听手机屏幕的按键=========================*/

    @Override
    public void onBackPressed() {
        boolean result = true;
        if (null != mCommonModelView) {
            result = mCommonModelView.onLifecycle(Lifecycle.onBackPressed);
        }
        if (result) {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (null != mCommonModelView) {
            Boolean flag = mCommonModelView.onKey(KeyEventConstant.onKeyDown, keyCode, event);
            if (flag != null) {
                return flag;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (null != mCommonModelView) {
            Boolean flag = mCommonModelView.onKey(KeyEventConstant.onKeyUp, keyCode, event);
            if (flag != null) {
                return flag;
            }
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        if (null != mCommonModelView) {
            Boolean flag = mCommonModelView.onKey(KeyEventConstant.onKeyLongPress, keyCode, event);
            if (flag != null) {
                return flag;
            }
        }
        return super.onKeyLongPress(keyCode, event);
    }

    @Override
    public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
        if (null != mCommonModelView) {
            Boolean flag = mCommonModelView.onKey(KeyEventConstant.onKeyMultiple, keyCode, repeatCount, event);
            if (flag != null) {
                return flag;
            }
        }
        return super.onKeyMultiple(keyCode, repeatCount, event);
    }

    @Override
    public boolean onKeyShortcut(int keyCode, KeyEvent event) {
        if (null != mCommonModelView) {
            Boolean flag = mCommonModelView.onKey(KeyEventConstant.onKeyShortcut, keyCode, event);
            if (flag != null) {
                return flag;
            }
        }
        return super.onKeyShortcut(keyCode, event);
    }

    @Override
    public void onProvideKeyboardShortcuts(List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
        if (null != mCommonModelView) {
            mCommonModelView.onProvideKeyboardShortcuts(data, menu, deviceId);
        }
        super.onProvideKeyboardShortcuts(data, menu, deviceId);
    }
    /*====================监听手机屏幕的按键=========================*/

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (null != mCommonModelView) {
            mCommonModelView.onConfigurationChanged(newConfig);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (null != mCommonModelView) {
            mCommonModelView.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (null != mCommonModelView) {
            mCommonModelView.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (null != mCommonModelView) {
            Boolean flag = mCommonModelView.onActivityTouchEvent(event);
            if (flag != null) {
                return flag;
            }
        }
        return super.onTouchEvent(event);
    }
}
